package com.ymttest.business.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.ymatou.app.parameter.AuthBean;
import com.ymatou.app.parameter.AuthForSellerBean;
import com.ymatou.app.seller.parameter.ActivityTabProductListBean;
import com.ymatou.app.seller.parameter.CreateActivity_V2Bean;
import com.ymatou.app.seller.parameter.GetActivityListBySellerBean;
import com.ymatou.app.seller.service.ActivityTabProductListCall;
import com.ymatou.app.seller.service.CreateActivity_V2Call;
import com.ymatou.app.seller.service.GetActivityListBySellerCall;
import com.ymatou.app.service.AuthCall;
import com.ymatou.app.service.AuthForSellerCall;
import com.ymt.core.tool.Logger;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/************************************************************************************
 * @File name :C2CCallService.java
 * @Author : zhouyi
 * @Date : 2015年8月13日
 * @Copyright : 洋码头
 ************************************************************************************/
public class C2CCallService {
    /**
     * 买家账号登入
     * 
     * @param UserName
     * @param Password
     * @return
     */

    private static String serverTime;

    public static String getLoginToken(String UserName, String Password) {
        String token = "";
        AuthBean ab = new AuthBean();
        AuthCall ac = new AuthCall();
        try {
            ab.setUserName(UserName);
            ab.setPassword(Password);
            ab.setDeviceToken(TestDataManager.getRandomUUID().toString());
            ac.setData(false, ab);
            ac.callService();
            token = ac.getAccessToken();
        } catch (Exception e) {
            Logger.debug(e.getMessage());
            Logger.debug(ac.getEntityString());
            Logger.debug(ac.getReturnData());
        }
        return token;
    }

    /**
     * 买手账号登入
     * 
     * @param UserName
     * @param Password
     * @return
     */
    public static String getSellerLoginToken(String UserName, String Password) {
        String token = "";
        AuthForSellerBean ab = new AuthForSellerBean();
        AuthForSellerCall ac = new AuthForSellerCall();
        try {
            ab.setUserName(UserName);
            ab.setPassword(Password);
            ab.setDeviceToken(TestDataManager.getRandomUUID().toString());
            ac.setData(false, ab);
            ac.callService();
            token = ac.getAccessToken();
        } catch (Exception e) {
            Logger.debug(e.getMessage());
            Logger.debug(ac.getEntityString());
            Logger.debug(ac.getReturnData());
        }
        return token;
    }

    /**
     * 买手创建一个直播活动
     * 
     * @param accessToken 买手
     * @return
     */
    public static void createActivity(String accessToken) {
        // 买手
        Map m = AuthCallServiceV2.signAuth(accessToken);
        int sellerId = (Integer) m.get("userId");
        int countryId = 0;
        int delType = 0;
        // 查询该买手能发是物流类型
        List<Map> catalogs = new DefaultWapper().selectCatalogStatusByCountryId(sellerId);
        if (catalogs.size() > 0) {
            int index = new Random().nextInt(catalogs.size() - 1);
            countryId = (Integer) catalogs.get(index).get("CountryId");
            delType = (Integer) catalogs.get(index).get("CatalogStatus");
        }
        CreateActivity_V2Bean createActivityBean = new CreateActivity_V2Bean();
        CreateActivity_V2Call createActivityCall = new CreateActivity_V2Call();
        try { // 设置参数
            createActivityBean.setAccessToken(accessToken);
            createActivityBean.setActivityContent("自动化测试TEST直播活动ActivityContent");
            createActivityBean.setActivityName("activityName");
            createActivityBean.setAppName("seller");
            createActivityBean.setBrands("asidas");
            createActivityBean.setCategoryId("10");
            createActivityBean.setCountryId(countryId);
            // 物流类型
            createActivityBean.setDelType(delType);
            // 当前时间后面3
            // createActivityBean.setStartTime(DateUtils.getBeforeOrNextSecond(3));
            createActivityBean.setStartTime(serverTime);
            createActivityBean.setEndTime(YMTDateUtil.getBeforeOrNextHour(5));
            createActivityBean.setMarketId("1153");
            createActivityBean
                    .setPicUrl("http://pc1.img.ymatou.com/G01/M00/09/B1/rBBlD1aWDK6AcJeTAAAIWScWAic788_c.jpg");
            createActivityBean.setPosition("中国上海市闸北区大宁路街道永和路187号");
            createActivityBean.setUserId(sellerId);
            // 发送数据
            createActivityCall.setData(false, createActivityBean);
            Logger.comment("买手创建一个直播活动");
            createActivityCall.callService();
            Logger.debug("createActivitycall send:" + createActivityCall.getEntityString());
            Logger.debug("createActivitycall response:" + createActivityCall.getReturnData());
            if (createActivityCall.getStatus() != 200) {
                Logger.debug("创建直播活动失败:" + createActivityCall.getMsg());
            }
        } catch (Exception e) {
            Logger.debug("创建一个直播活动失败" + e.getStackTrace());
        }
    }

    /**
     * 根据买手token查询当前正在直播的活动id
     * 
     * @param accessToken 买手token
     * @return
     */
    public static int getActivityIdBySeller(String accessToken) {
        GetActivityListBySellerBean getActivityListbean = new GetActivityListBySellerBean();
        GetActivityListBySellerCall getActivityListcall = new GetActivityListBySellerCall();
        int activityId = -1;
        try { // 设置参数
              // 查询当前是否有有效的直播活动
            getActivityListbean.setAccessToken(accessToken);
            // 发送数据
            getActivityListcall.setData(false, getActivityListbean);
            Logger.comment("查询买手当前直播活动列表");
            getActivityListcall.callService();
            Logger.debug("getActivityListcall send:" + getActivityListcall.getEntityString());
            Logger.debug("getActivityListcall response:" + getActivityListcall.getReturnData());
            // 取C2C server时间，直播开始的时间为服务器时间后5s
            Date time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(getActivityListcall.getString("ServerTime"));
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(time);
            calendar.add(calendar.SECOND, 3);
            serverTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
            // 直播状态(-1:未开始 0:进行中 1:已结束)
            int lastActivityStatus = getActivityListcall.getLastActivityStatus();
            switch (lastActivityStatus) {
                case -1:
                    activityId = -1;
                    break;
                case 0:
                    activityId = getActivityListcall.getLastActivityId();
                    break;
                case 1:
                    activityId = 1;
                    break;
            }
        } catch (Exception e) {
            Logger.debug("根据买手查询直播活动失败" + e.getStackTrace());
        }
        return activityId;
    }

    /**
     * 根据买手id查询当前直播的商品id
     * 
     * @param accessToken 买手token
     * @param activityId 买手发布的直播活动id
     * @return
     */
    public static String getActivityProductIdBySeller(String accessToken, int activityId) {
        String priductId = null;
        try { // 设置参数
            ActivityTabProductListBean activitytabproductlistBean = new ActivityTabProductListBean();
            ActivityTabProductListCall activitytabproductlistCall = new ActivityTabProductListCall();

            activitytabproductlistBean.setAccessToken(accessToken);
            activitytabproductlistBean.setActivityId(activityId);
            activitytabproductlistBean.setIsReplay(false);
            activitytabproductlistBean.setProductNum(1);
            // 已上架
            activitytabproductlistBean.setTabName("Putaway");
            activitytabproductlistCall.setData(false, activitytabproductlistBean);
            Logger.comment("查询直播活动:" + activityId + "下的直播商品");
            activitytabproductlistCall.callService();
            Logger.debug("activitytabproductlistCall send:" + activitytabproductlistCall.getEntityString());
            Logger.debug("activitytabproductlistCall response:" + activitytabproductlistCall.getReturnData());
            priductId = activitytabproductlistCall.getProductId();
        } catch (Exception e) {
            Logger.debug("根据买手查询直播商品失败" + e.getStackTrace());
        }
        return priductId;
    }

    // c2c接口重构压测数据准备
    public static void main(String args[]) {
    	String accessToken=new AuthCallServiceV2().authLogin("juicyjuicy", "123456").get("accessToken").toString();
		new C2CCallService().createActivity(accessToken);
//        ArrayList<String[]> slist = TestDataManager.getData("sellerlist");
//        for (String[] s : slist) {
//            if (s.length >= 2) {
//                try {
//                    new ULC201406Wapper().updatePwdByUid(Integer.valueOf(s[0]));
//                } catch (NumberFormatException e) {
//                    e.printStackTrace();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                // String token = C2CCallService.getLoginToken(s[1], "123456");
//                String token = C2CCallService.getSellerLoginToken(s[1], "123456");
//                if (token != null) {
//                    if (token.length() > 10) {
//                        if (!s[0].equals("1113")) {
//                            // TestDataManager.appendFileToLine("C2CToken", s[0]
//                            // + "," + s[1] + "," + s[2] + ","+ token);
//                            TestDataManager.appendFileToLine("C2CSellerToken", s[0] + "," + s[1] + "," + token);
//                        }
//                    }
//                }
//            }
//        }
//
//        ArrayList<String[]> ulist = TestDataManager.getData("userlist");
//        for (String[] s : ulist) {
//            if (s.length >= 2) {
//                try {
//                    new ULC201406Wapper().updatePwdByUid(Integer.valueOf(s[0]));
//                } catch (NumberFormatException e) {
//                    e.printStackTrace();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                String token = C2CCallService.getLoginToken(s[1], "123456");
//                // String token = C2CCallService.getSellerLoginToken(s[1],
//                // "123456");
//                if (token != null) {
//                    if (token.length() > 10) {
//                        if (!s[0].equals("1113")) {
//                            TestDataManager.appendFileToLine("C2CToken", s[0] + "," + s[1] + "," + s[2] + "," + token);
//                            // TestDataManager.appendFileToLine("C2CSellerToken",
//                            // s[0] + "," + s[1] + "," + s[2] + ","+ token);
//                        }
//                    }
//                }
//            }
//        }
//
    }
}
